# This file codes all main individual level variables
# based on crime order presented in each focus group
# It codes private, public, group choice
# and post-discussion private preferences at the 
# individual level. 
# This file also recodes the severity scale
# to reflect perceptions of punishment severity
# Finally it calculated distance between respondent
# private preferences and the wider focus group preference
# before and after discussions.

#Insert correct file path
D <- read.csv("Data.IndividualLevel.csv")
D2 <- read.csv("Data.FGPunishmentRankings.csv")
D3 <- merge(D, D2, by = c("VILLID", "FGID"),
            all = TRUE)

#CODE BASICE VARIABLES FOR ANALYSIS ---------------------------------------------

# RETREIVE ORDER VARIABLES FROM D2 DATA ON FG QUAL SHEET
D3$LEVIOL.FG[D3$C1=="VIOL"] <-1
D3$LEVIOL.FG[D3$C2=="VIOL"] <-2
D3$LEVIOL.FG[D3$C3=="VIOL"] <-3

D3$LEVOL.FG[D3$C1=="VOL"] <-1
D3$LEVOL.FG[D3$C2=="VOL"] <-2
D3$LEVOL.FG[D3$C3=="VOL"] <-3

D3$LAVIOLENCEDOMESTIQUE.FG[D3$C1=="DV"] <-1
D3$LAVIOLENCEDOMESTIQUE.FG[D3$C2=="DV"] <-2
D3$LAVIOLENCEDOMESTIQUE.FG[D3$C3=="DV"] <-3

#coding crime/punishment variables:PRIVATE 1
D3$PRIV1.VOL[D3$LEVOL.FG==1]<-D3$PRI1C1[D3$LEVOL.FG==1]
D3$PRIV1.VOL[D3$LEVOL.FG==2]<-D3$PRI1C2[D3$LEVOL.FG==2]
D3$PRIV1.VOL[D3$LEVOL.FG==3]<-D3$PRI1C3[D3$LEVOL.FG==3]

D3$PRIV1.DV[D3$LAVIOLENCEDOMESTIQUE.FG==1]<-D3$PRI1C1[D3$LAVIOLENCEDOMESTIQUE.FG==1]
D3$PRIV1.DV[D3$LAVIOLENCEDOMESTIQUE.FG==2]<-D3$PRI1C2[D3$LAVIOLENCEDOMESTIQUE.FG==2]
D3$PRIV1.DV[D3$LAVIOLENCEDOMESTIQUE.FG==3]<-D3$PRI1C3[D3$LAVIOLENCEDOMESTIQUE.FG==3]

D3$PRIV1.V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LAVIOLENCEDOMESTIQUE.FG==2]<-D3$PUB1C2[D3$LAVIOLENCEDOMESTIQUE.FG==2]
D3$PUB1.DV[D3$LAVIOLENCEDOMESTIQUE.FG==3]<-D3$PUB1C3[D3$LAVIOLENCEDOMESTIQUE.FG==3]

D3$PUB1.VIOL[D3$LEVIOL.FG==1]<-D3$PUB1C1[D3$LEVIOL.FG==1]
D3$PUB1.VIOL[D3$LEVIOL.FG==2]<-D3$PUB1C2[D3$LEVIOL.FG==2]
D3$PUB1.VIOL[D3$LEVIOL.FG==3]<-D3$PUB1C3[D3$LEVIOL.FG==3]

#coding crime/punishment variables: GROUP CHOICE
D3$GCHOIX.VOL[D3$LEVOL.FG==1]<-D3$GCHOIX1[D3$LEVOL.FG==1]
D3$GCHOIX.VOL[D3$LEVOL.FG==2]<-D3$GCHOIX2[D3$LEVOL.FG==2]
D3$GCHOIX.VOL[D3$LEVOL.FG==3]<-D3$GCHOIX3[D3$LEVOL.FG==3]
#table(D3$source[D3$GCHOIX1==10])
#error in D5_FG1_LUHUNDU.xlsx 
D3$GCHOIX.VOL[D3$GCHOIX1==10] <- 1 

D3$GCHOIX.DV[D3$LAVIOLENCEDOMESTIQUE.FG==1]<-D3$GCHOIX1[D3$LAVIOLENCEDOMESTIQUE.FG==1]
D3$GCHOIX.DV[D3$LAVIOLENCEDOMESTIQUE.FG==2]<-D3$GCHOIX2[D3$LAVIOLENCEDOMESTIQUE.FG==2]
D3$GCHOIX.DV[D3$LAVIOLENCEDOMESTIQUE.FG==3]<-D3$GCHOIX3[D3$LAVIOLENCEDOMESTIQUE.FG==3]

D3$GCHOIX.VIOL[D3$LEVIOL.FG==1]<-D3$GCHOIX1[D3$LEVIOL.FG==1]
D3$GCHOIX.VIOL[D3$LEVIOL.FG==2]<-D3$GCHOIX2[D3$LEVIOL.FG==2]
D3$GCHOIX.VIOL[D3$LEVIOL.FG==3]<-D3$GCHOIX3[D3$LEVIOL.FG==3]
# 
# 
# ### ANALYSIS OF NEW ORDERING OF PUNISHMENT VARIABLES
# 
# mean(D3$P1, na.rm=TRUE)
# mean(D3$P2, na.rm=TRUE)
# mean(D3$P3, na.rm=TRUE)
# mean(D3$P4, na.rm=TRUE)
# mean(D3$P5, na.rm=TRUE)
# 
# mean(D3$P1[D3$FEMALE==1], na.rm=TRUE)
# mean(D3$P2[D3$FEMALE==1], na.rm=TRUE)
# mean(D3$P3[D3$FEMALE==1], na.rm=TRUE)
# mean(D3$P4[D3$FEMALE==1], na.rm=TRUE)
# mean(D3$P5[D3$FEMALE==1], na.rm=TRUE)
# #PRISON MUCH WORSE THAN BEATING FOR WOMEN (.8 DIFFERENCE)
# 
# mean(D3$P1[D3$FEMALE==0], na.rm=TRUE)
# mean(D3$P2[D3$FEMALE==0], na.rm=TRUE)
# mean(D3$P3[D3$FEMALE==0], na.rm=TRUE)
# mean(D3$P4[D3$FEMALE==0], na.rm=TRUE)
# mean(D3$P5[D3$FEMALE==0], na.rm=TRUE)
# # PRISON AND BEATING SAME FOR MEN (.02 DIFFERENCE)
# 
# median(D3$P1, na.rm=TRUE)
# median(D3$P2, na.rm=TRUE)
# median(D3$P3, na.rm=TRUE)
# median(D3$P4, na.rm=TRUE)
# median(D3$P5, na.rm=TRUE)
# 
# ## TABLE FOR MEANS
# 
# r1 <- round(data.frame(cbind(
#   mean(D2$P1[D3$FEMALE==1], na.rm=TRUE),
#   mean(D2$P2[D3$FEMALE==1], na.rm=TRUE),
#   mean(D2$P3[D3$FEMALE==1], na.rm=TRUE),
#   mean(D2$P4[D3$FEMALE==1], na.rm=TRUE),
#   mean(D2$P5[D3$FEMALE==1], na.rm=TRUE))),3)
# r2 <- round(data.frame(cbind(
#   sd(D2$P1[D3$FEMALE==1], na.rm=TRUE),
#   sd(D2$P2[D3$FEMALE==1], na.rm=TRUE),
#   sd(D2$P3[D3$FEMALE==1], na.rm=TRUE),
#   sd(D2$P4[D3$FEMALE==1], na.rm=TRUE),
#   sd(D2$P5[D3$FEMALE==1], na.rm=TRUE))),3)
# r3 <- round(data.frame(cbind(
#       mean(D2$P1[D3$FEMALE==0], na.rm=TRUE),
#       mean(D2$P2[D3$FEMALE==0], na.rm=TRUE),
#       mean(D2$P3[D3$FEMALE==0], na.rm=TRUE),
#       mean(D2$P4[D3$FEMALE==0], na.rm=TRUE),
#       mean(D2$P5[D3$FEMALE==0], na.rm=TRUE))),3)
# r4 <- round(data.frame(cbind(
#   sd(D2$P1[D3$FEMALE==0], na.rm=TRUE),
#   sd(D2$P2[D3$FEMALE==0], na.rm=TRUE),
#   sd(D2$P3[D3$FEMALE==0], na.rm=TRUE),
#   sd(D2$P4[D3$FEMALE==0], na.rm=TRUE),
#   sd(D2$P5[D3$FEMALE==0], na.rm=TRUE))),3)
# mat <- data.frame(rbind(r1, r2, r3, r4))
# names(mat) <- c("Counsel", "Payment", "Expulsion", "Prison", "Beating")
# rownames(mat) <- c("Women (Means)", "Women (SD)","Men (Means)", "Men (SD)")
# 
# 
# mat.sd <- data.frame(rbind(r1, r2))
# names(mat.sd) <- c("Counsel", "Payment", "Expulsion", "Prison", "Beating")
# rownames(mat.sd) <- c("Women", "Men")

## COLLAPSE SEVERITY LEVELS 4 AND 4
### VIOL VARIABLES WITH SCALE OF 1-4 FOR BOTH MEN AND WOMEN
D3$PRIV1.RC2.VIOL <- D3$PRIV1.VIOL
D3$PRIV2.RC2.VIOL<- D3$PRIV2.VIOL
D3$PUB1.RC2.VIOL <- D3$PUB1.VIOL
D3$GCHOIX.RC2.VIOL<- D3$GCHOIX.VIOL

D3$PRIV1.RC2.VIOL[(D3$PRIV1.VIOL==5)] <- 4
D3$PRIV2.RC2.VIOL[(D3$PRIV2.VIOL==5)] <- 4
D3$PUB1.RC2.VIOL[(D3$PUB1.VIOL==5)] <- 4
D3$GCHOIX.RC2.VIOL[(D3$GCHOIX.VIOL==5)] <- 4

### VOL VARIABLES WITH SCALE OF 1-4 FOR BOTH MEN AND WOMEN
D3$PRIV1.RC2.VOL <- D3$PRIV1.VOL
D3$PRIV2.RC2.VOL<- D3$PRIV2.VOL
D3$PUB1.RC2.VOL <- D3$PUB1.VOL
D3$GCHOIX.RC2.VOL<- D3$GCHOIX.VOL

D3$PRIV1.RC2.VOL[(D3$PRIV1.VOL==5)] <- 4
D3$PRIV2.RC2.VOL[(D3$PRIV2.VOL==5)] <- 4
D3$PUB1.RC2.VOL[(D3$PUB1.VOL==5)] <- 4
D3$GCHOIX.RC2.VOL[(D3$GCHOIX.VOL==5)] <- 4

### DV VARIABLES WITH SCALE OF 1-4 FOR BOTH MEN AND WOMEN
D3$PRIV1.RC2.DV <- D3$PRIV1.DV
D3$PRIV2.RC2.DV<- D3$PRIV2.DV
D3$PUB1.RC2.DV <- D3$PUB1.DV
D3$GCHOIX.RC2.DV<- D3$GCHOIX.DV

D3$PRIV1.RC2.DV[(D3$PRIV1.DV==5)] <- 4
D3$PRIV2.RC2.DV[(D3$PRIV2.DV==5)] <- 4
D3$PUB1.RC2.DV[(D3$PUB1.DV==5)] <- 4
D3$GCHOIX.RC2.DV[(D3$GCHOIX.DV==5)] <- 4


#------------------------------------------------------
#Create a new variable calculating the distance (absolute value) between private preference for each individual 
# and the mean preference of the focus group

D3$FGMEAN.PRIV1.VIOL <- D3$FGMEAN.PRIV1.VOL <- D3$FGMEAN.PRIV1.DV <-  NA
D3$FGMEAN.PRIV2.VIOL <- D3$FGMEAN.PRIV2.VOL <- D3$FGMEAN.PRIV2.DV <-  NA
for (i in 1:nrow(D3)){
  #CALCUATE ABSOLUTE VALUE OF DISTANCE FOR EACH CRIME
  D3$FGMEAN.PRIV1.VIOL[i] <- abs(D3$PRIV1.RC2.VIOL[i] - mean(D3$PRIV1.RC2.VIOL[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
  D3$FGMEAN.PRIV1.VOL[i] <- abs(D3$PRIV1.RC2.VOL[i] - mean(D3$PRIV1.RC2.VOL[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
  D3$FGMEAN.PRIV1.DV[i] <- abs(D3$PRIV1.RC2.DV[i] - mean(D3$PRIV1.RC2.DV[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
  
  D3$FGMEAN.PRIV2.VIOL[i] <- abs(D3$PRIV2.RC2.VIOL[i] - mean(D3$PRIV2.RC2.VIOL[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
  D3$FGMEAN.PRIV2.VOL[i] <- abs(D3$PRIV2.RC2.VOL[i] - mean(D3$PRIV2.RC2.VOL[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
  D3$FGMEAN.PRIV2.DV[i] <- abs(D3$PRIV2.RC2.DV[i] - mean(D3$PRIV2.RC2.DV[D3$VFGID3==D3$VFGID3[i]], na.rm=TRUE))
}

